package com.example.demo.controller;

import com.example.demo.common.domain.*;
import com.example.demo.common.util.Result;
import com.example.demo.service.ComLeaveService;
import com.example.demo.service.LeaveService;
import com.example.demo.service.OutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/deputy")
public class DeputyController {
    @Autowired
    @Qualifier("leaveServiceImpl")
    LeaveService leaveService;
    @Autowired
    OutService outService;
    @Autowired
    ComLeaveService comLeaveService;
    public boolean check(HttpSession session){
        DeputyManager deputyManager = (DeputyManager)session.getAttribute("deputyManager");
        if(deputyManager != null){
            return true;
        }
        return false;
    }

    @RequestMapping("/agreeLeaveItem")
    @ResponseBody//根据传入的请假号，更新该请假表单中的字段 此处为副经理ID和副经理同意
    public Map agreeLeaveItem(HttpSession session, Integer lid){
        DeputyManager deputyManager = (DeputyManager)session.getAttribute("deputyManager");
        leaveService.updateDIdByPrimaryId(lid,deputyManager.getDid());
        return Result.success(true);
    }
    @RequestMapping("/agreeOutItem")
    @ResponseBody//根据传入的外出号，更新外出表单中的字段 此处为副经理ID和副经理同意
    public Map agreeOutItem(HttpSession session, Integer oid){
        DeputyManager deputyManager = (DeputyManager)session.getAttribute("deputyManager");
        outService.updateDIdByPrimaryId(oid,deputyManager.getDid());
        return Result.success(true);
    }

    @RequestMapping("/agreeComLeaveItem")
    @ResponseBody//根据传入的调休号，更新外出表单中的字段 此处为副经理ID和副经理同意
    public Map agreeComLeaveItem(HttpSession session, Integer cid){
        DeputyManager deputyManager = (DeputyManager)session.getAttribute("deputyManager");
        comLeaveService.updateDIdByPrimaryId(cid,deputyManager.getDid());
        return Result.success(true);
    }
    @RequestMapping("/agreeleave")//搜索部门经理通过的请假申请表单，跳转到副经理同意请假申请页面
    public String agreeleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                             @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            DeputyManager deputyManager = new DeputyManager();
            deputyManager.setDid(2);
            deputyManager.setDname("李副经理");
            session.setAttribute("deputyManager",deputyManager);
        }

        List<LeaveRecordPlus> leave_records = leaveService.DeputySelectAllLeave();//连接查询 请假表和员工信息表一起查询
        model.addAttribute("list",leave_records);
        return "manger/deputy/leavelist";
    }

    @RequestMapping("/agreeout")//搜索部门经理通过的外出申请表单，跳转到副经理同意外出申请页面
    public String agreeout(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            DeputyManager deputyManager = new DeputyManager();
            deputyManager.setDid(2);
            deputyManager.setDname("李副经理");
            session.setAttribute("deputyManager",deputyManager);
        }

        List<OutRecordPlus> recordPluses = outService.DeputySelectAllOut();//连接查询 请假表和员工信息表一起查询
        System.out.println(recordPluses.size());
        model.addAttribute("list",recordPluses);
        return "manger/deputy/outlist";
    }
    @RequestMapping("/agreecomleave")//搜索部门经理通过的调休申请表单，跳转到副经理同意调休申请页面
    public String agreecomleave(HttpSession session, Model model, @RequestParam(required = false,defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false,defaultValue = "6") Integer pageSize){
        if(!check(session)){
            DeputyManager deputyManager = new DeputyManager();
            deputyManager.setDid(2);
            deputyManager.setDname("李副经理");
            session.setAttribute("deputyManager",deputyManager);
        }

        List<CompensatoryLeavePlus> compensatoryLeavePluses = comLeaveService.selectAllPlus();
        model.addAttribute("list",compensatoryLeavePluses);
        return "manger/deputy/comleavelist";
    }
}
